// pages/register/index.js
import api from '../../request/api.js'
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    color: '#1CC4AD',
    disabled: false,
    getCode: '获取验证码',
    user_id:'',
    model:{
      phone:'',
      sms:'',
      password:'',
      surePassword:''
    },
    computeTime:0 ,//倒计时

  },
  bindKeyInput: function (e) {
    console.dir(e)
    // this.setData({
    //   inputValue: e.detail.value
    // })
  },
  getInputPhone: function (e) {
    // console.log(e)
    let phone = 'model.phone'
    this.setData({
      [phone]: e.detail.value
    })
  },
  async formSubmit(e){
    console.dir(e)
    let password = e.detail.value.password
    let phone = e.detail.value.phone
    let repwd = e.detail.value.surePassword
    let code = e.detail.value.sms
    let user_id = this.data.user_id
    if(!code){Toast('请输入验证码')}
    let data = {
      user_id,
      phone,
      code
    }
    const result = await api('POST', '/Recycle/user/bindPhone',
      data
    )
    console.error(result);
    if(result.data.code == 200){
      wx.setStorageSync('token', result.data.data.token)
      wx.setStorageSync('userInfo', result.data.data)
      wx.switchTab({
        url: '../index/index',
      })
    }else if(result.data.code == 201){
      wx.navigateTo({
        url: '../ImproveInformation/index?user_id=' + result.data.data,
      })
    }else{
      Toast(result.msg);
    }
  },

  // 异步获取短信验证码
  async sendCode() {
    let { computeTime} = this.data
    let {phone} = this.data.model
    let rightPhone = /^1\d{10}$/.test(phone)
    if (!rightPhone) {
      Toast('手机号码不正确')
      return;
    } 
    console.dir(!computeTime)
    // 如果当前没有计时
    if (!computeTime) {
      // 启动倒计时
      computeTime = 30
      this.intervalId = setInterval(() => {
        if (computeTime <= 0) {
          // 停止计时
          clearInterval(this.intervalId)
        }
        console.log(computeTime)
        this.setData({
          computeTime: computeTime--,
        })

      }, 1000)
      // 发送ajax请求(向指定手机号发送验证码短信)
      let data = {
            phone,
            flag: 4
          }
      const result = await api('POST','/Recycle/user/sendSms',
        data
      )
      if (result.data.code === 200) {
        console.log(result)
        // 显示提示
        // 停止计时
        // if (computeTime) {
        //   this.computeTime = 0
        //   clearInterval(this.intervalId)
        //   this.intervalId = undefined
        // }
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.user_id){
      this.setData({
        user_id : options.user_id
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})